package org.song.videoplayer.floatwindow;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
/**
 * Created by song on 2018/1/15.
 * Contact github.com/tohodog
 * 浮窗管理
 */

public class WindowManage {


    public WindowManage(Context context) {
        getWindowManager(context);
    }

    public int w, h;

    /**
     * 添加悬浮窗
     */
    public void addWindowView(ComponentContainer view) {
        WindowManager windowManager = getWindowManager(view.getContext());
        if (windowManager != null)
            windowManager.addComponent(view, view.getContext(),WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
    }

    /**
     * 移除悬浮窗
     */
    public void removeWindowView(Component view) {
        WindowManager windowManager = getWindowManager(view.getContext());
        if (windowManager != null)
            windowManager.destroyWindow(windowManager.getTopWindow().get());
    }

    /**
     * 更新悬浮窗
     */
    public void updateWindowView(ComponentContainer view) {
        WindowManager windowManager = getWindowManager(view.getContext());
        if (windowManager != null){
            windowManager.destroyWindow(windowManager.getTopWindow().get());
            windowManager.addComponent(view, view.getContext(),WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
        }
    }


    public WindowManager.LayoutConfig creatParams(int type, FloatParams floatParams) {
        //系统浮窗不能超出边界,更新xy
        int ww = (w - floatParams.w) / 2;
        int hh = (h - floatParams.h) / 2;
        if (Math.abs(floatParams.x) > ww)
            floatParams.x = floatParams.x > 0 ? ww : -ww;
        if (Math.abs(floatParams.y) > hh)
            floatParams.y = floatParams.y > 0 ? hh : -hh;

        WindowManager.LayoutConfig smallWindowParams = new WindowManager.LayoutConfig();
        smallWindowParams.type = type;
//        smallWindowParams.alpha = PixelFormat.ARGB_8888;
        smallWindowParams.flags = WindowManager.LayoutConfig.MARK_TOUCHABLE_IMPOSSIBLE
                | WindowManager.LayoutConfig.MARK_FOCUSABLE_IMPOSSIBLE;
        //smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
        smallWindowParams.width = floatParams.w;
        smallWindowParams.height = floatParams.h;
        smallWindowParams.x = floatParams.x;
        smallWindowParams.y = floatParams.y;
        return smallWindowParams;
    }

    private WindowManager windowManager;

    private WindowManager getWindowManager(Context context) {
        if (windowManager == null) {
            windowManager = WindowManager.getInstance();
        }
        DisplayAttributes displayAttributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getRealAttributes();
        if (displayAttributes != null) {
            h = displayAttributes.height;
            w = displayAttributes.width;
        }
        return windowManager;
    }


}
